#!/bin/sh
# Copyright (C) 2024-2025 KoudaiPC Computer Technologies Co., Ltd.
# script name: huangdi-setup-environment.sh
# author: david921518@qq.com

################
# global vars
GLB_ROOT_DEV=/dev/sda
################

opkg_install_pkg()
{
  PKG_NAME=$1
  echo "opkg install $PKG_NAME" 2>&1 | tee /dev/kmsg

  if [ "${PKG_NAME}"x == x ]; then
    echo "Error: PKG_NAME is empty!!!" 2>&1 | tee /dev/kmsg
    while true; do reboot; sleep 9; done
  fi

  opkg install $PKG_NAME 2>&1 | tee /dev/kmsg

  let COUNTER=0
  PKG_INSTALLED=$(opkg list-installed | grep "^$PKG_NAME -" | awk '{print $1}')
  while [ "${PKG_INSTALLED}"x != "${PKG_NAME}"x ]; do
    echo "Try to install $PKG_NAME again" 2>&1 | tee /dev/kmsg
    sleep 1
    let COUNTER=$COUNTER+1
    if [ $COUNTER -gt 29 ]; then
      echo "Can't install $PKG_NAME , reboot now" 2>&1 | tee /dev/kmsg
      while true; do reboot; sleep 9; done
    fi
    opkg update 2>&1 | tee /dev/kmsg
    opkg install $PKG_NAME 2>&1 | tee /dev/kmsg
    PKG_INSTALLED=$(opkg list-installed | grep "^$PKG_NAME -" | awk '{print $1}')
  done
}

opkg_remove_force_depends_pkg()
{
  PKG_NAME=$1
  echo "opkg remove --force-depends $PKG_NAME" 2>&1 | tee /dev/kmsg

  if [ "${PKG_NAME}"x == x ]; then
    echo "Error: PKG_NAME is empty!!!" 2>&1 | tee /dev/kmsg
    while true; do reboot; sleep 9; done
  fi

  opkg remove --force-depends $PKG_NAME 2>&1 | tee /dev/kmsg

  let COUNTER=0
  PKG_REMOVED=$(opkg list-installed | grep "^$PKG_NAME -" | awk '{print $1}')
  while [ "$PKG_REMOVED"x == "$PKG_NAME"x ]; do
    echo "Try to remove $PKG_NAME again" 2>&1 | tee /dev/kmsg
    sleep 1
    let COUNTER=$COUNTER+1
    if [ $COUNTER -gt 29 ]; then
      echo "Can't remove $PKG_NAME , reboot now" 2>&1 | tee /dev/kmsg
      while true; do reboot; sleep 9; done
    fi
    opkg remove --force-depends $PKG_NAME 2>&1 | tee /dev/kmsg
    PKG_REMOVED=$(opkg list-installed | grep "^$PKG_NAME -" | awk '{print $1}')
  done
}

# 1. setup WAN interface
uci set network.wan=interface
uci set network.wan.device=eth0
uci set network.wan.proto=dhcp
uci commit

# 2. setup device bridge br-lan ports
uci delete network.@device[0].ports
uci add_list network.@device[0].ports=dummy0
uci commit

# 3. restart network service
service network restart
sleep 1
let COUNTER=0
WAN_IP=$(ubus call network.interface.wan status | grep "address" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
while [ "${WAN_IP}"x == x ]; do
  sleep 1
  let COUNTER=$COUNTER+1
  if [ $COUNTER -gt 29 ]; then
    echo "Can't get WAN IP address, reboot now" 2>&1 | tee /dev/kmsg
    while true; do reboot; sleep 9; done
  fi
  WAN_IP=$(ubus call network.interface.wan status | grep "address" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
done

echo "[setup-environment] WAN IP=[${WAN_IP}]" 2>&1 | tee /dev/kmsg
sleep 1
echo "[setup-environment] PATH=[$PATH]" 2>&1 | tee /dev/kmsg
sleep 1

#opkg remove --force-depends uhttpd
#opkg remove --force-depends dnsmasq
opkg_remove_force_depends_pkg uhttpd
opkg_remove_force_depends_pkg dnsmasq

# 4. install kmod-dummy
sed "s#downloads.openwrt.org#mirrors.aliyun.com/openwrt#g" -i /etc/opkg/distfeeds.conf
opkg update 2>&1 | tee /dev/kmsg
#opkg install kmod-dummy 2>&1 | tee /dev/kmsg
opkg_install_pkg kmod-dummy

echo "[setup-environment] INSTALL TOOLS" 2>&1 | tee /dev/kmsg
sleep 1

# 5. install toosl for enlarging partition size
#opkg install parted 2>&1 | tee /dev/kmsg
#opkg install losetup 2>&1 | tee /dev/kmsg
#opkg install lsblk 2>&1 | tee /dev/kmsg
#opkg install resize2fs 2>&1 | tee /dev/kmsg
opkg_install_pkg parted
opkg_install_pkg losetup
opkg_install_pkg lsblk
opkg_install_pkg resize2fs

let COUNTER=0
while [ ! -e $GLB_ROOT_DEV ]; do
  sleep 1
  let COUNTER=$COUNTER+1
  if [ $COUNTER -gt 29 ]; then
    echo "Can't find $GLB_ROOT_DEV , reboot now" 2>&1 | tee /dev/kmsg
    while true; do reboot; sleep 9; done
  fi
  echo "Check $GLB_ROOT_DEV again" 2>&1 | tee /dev/kmsg
done

# 6. enlarge partition
export loop_device=$(losetup -f)
losetup $loop_device $GLB_ROOT_DEV
echo -e "Fix" | parted ---pretend-input-tty "$loop_device" print
parted "$loop_device" resizepart 2 100%
parted "$loop_device" print 2>&1 | tee /dev/kmsg
sleep 1

sync
sleep 1

echo "[setup-environment] EXIT" 2>&1 | tee /dev/kmsg
sleep 1

# 7. restart system
#poweroff

